﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation
{
    /// <summary>
    /// API接口输出统一格式
    /// </summary>
    [Serializable]
    public class ApiResult : ApiResult<object>
    {
        /// <summary>
        /// 生成API接口正确输出实体
        /// </summary>
        /// <param name="data">数据实体</param>
        /// <returns></returns>
        public static new ApiResult ToSuccess(object data)
        {
            return new ApiResult() { status = 200, data = data };
        }
        /// <summary>
        /// 生成API接口正确输出实体
        /// </summary>
        /// <returns></returns>
        public static new ApiResult ToSuccess()
        {
            return new ApiResult() { status = 200, data = null };
        }
        /// <summary>
        /// 生成API接口错误输出
        /// </summary>
        /// <param name="errorCode">错误码</param>
        /// <param name="errorMsg">错误信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static new ApiResult ToFail(int errorCode, string errorMsg, object data)
        {
            return new ApiResult() { status = errorCode, msg = errorMsg, data = data };
        }
        /// <summary>
        /// 生成API接口错误输出
        /// </summary>
        /// <param name="errorCode">错误码</param>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static new ApiResult ToFail(int errorCode, string errorMsg)
        {
            return ToFail(errorCode, errorMsg, null);
        }

        /// <summary>
        /// 生成API接口错误输出（默认错误码为500）
        /// </summary>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static new ApiResult ToFail(string errorMsg)
        {
            return ToFail(500, errorMsg);
        }

        /// <summary>
        /// 生成返回对象
        /// </summary>
        /// <param name="success">是否成功</param>
        /// <param name="data">数据</param>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static ApiResult ToApiResult(bool success, object? data=null, string? errorMsg=null)
        {
            if (success)
            {
                return ToSuccess(data);
            }
            else
            {
                return ToFail(500, errorMsg, data);
            }

        }
    }

    /// <summary>
    /// API接口输出统一格式
    /// </summary>
    [Serializable]
    public class ApiResult<T> : ApiResultBase where T : class
    {


        /// <summary>
        /// 返回数据
        /// </summary>
        public new T? data { get; set; }

        /// <summary>
        /// 生成API接口正确输出实体
        /// </summary>
        /// <param name="data">数据实体</param>
        /// <returns></returns>
        public static ApiResult<T> ToSuccess(T data)
        {
            return new ApiResult<T>() { status = 200, data = data };
        }
        /// <summary>
        /// 生成API接口正确输出实体
        /// </summary>
        /// <returns></returns>
        public static new ApiResult ToSuccess()
        {
            return new ApiResult() { status = 200, data = null };
        }
        /// <summary>
        /// 生成API接口错误输出
        /// </summary>
        /// <param name="errorCode">错误码</param>
        /// <param name="errorMsg">错误信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResult<T> ToFail(int errorCode, string errorMsg, T data)
        {
            return new ApiResult<T>() { status = errorCode, msg = errorMsg, data = data };
        }
        /// <summary>
        /// 生成API接口错误输出
        /// </summary>
        /// <param name="errorCode">错误码</param>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static ApiResult<T> ToFail(int errorCode, string errorMsg)
        {
            return ToFail(errorCode, errorMsg, default(T));
        }

        /// <summary>
        /// 生成API接口错误输出（默认错误码为500）
        /// </summary>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static ApiResult<T> ToFail(string errorMsg)
        {
            return ToFail(500, errorMsg);
        }

        /// <summary>
        /// 生成返回对象
        /// </summary>
        /// <param name="success">是否成功</param>
        /// <param name="data">数据</param>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static ApiResult<T> ToApiResult(bool success,T? data,string? errorMsg)
        {
            if(success)
            {
                return ToSuccess(data);
            }
            else
            {
                return ToFail(500, errorMsg, data);
            }
            
        }
    }
    /// <summary>
    /// API接口输出基类
    /// </summary>
    public abstract class ApiResultBase
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int status { get; set; } = 200;

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? msg { get; set; }
    }
}
